package com.cy.personer.web;

import com.cy.personer.bean.*;
import com.cy.personer.biz.impl.OperationUserBizImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/operationUser")
public class operationUserHandler {
    @Resource
    private OperationUserBizImpl operationUserBizImpl;

    //显示输入卡号页面
    @RequestMapping(value="/showUser.action")
    public String showUser(){
        return "admin/operationUser";
    }

    //检测该卡号是否存在
    @RequestMapping(value = "/checkCard.action",method=RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody
    Map<String,Object> /*String*/ checkCard(HttpServletRequest request, String cardNumber){
        List<RoleUserOptions> allRoleUserOptions = null;
        Map<String,Object> map = new HashMap<>();
        HttpSession session = request.getSession();
        User user = operationUserBizImpl.getUser(cardNumber);
        if (user!=null){
            Work work1 = (Work) request.getSession().getAttribute("work");
            session.setAttribute("user",user);
//            map.put("user",user);
            allRoleUserOptions = operationUserBizImpl.getRoleUserOptions(work1.getRoleId());
//            map.put("allRoleUserOptions",allRoleUserOptions);
        }
        map.put("user",user);
        map.put("allRoleUserOptions",allRoleUserOptions);
        return map;
    }

    //展示为用户充值页面
    @RequestMapping(value="/showRecharge.action")
    public String showRecharge(){
        return "admin/recharge";
    }

    //为用户充值
    @RequestMapping(value="/recharge.action",method=RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody String recharge(HttpServletRequest request,double userMoney1){
        String result1 = "error";
        HttpSession session = request.getSession();
        User user1 = (User) request.getSession().getAttribute("user");
        Work work = (Work) request.getSession().getAttribute("work");
        MoneyDetails moneyDetails = new MoneyDetails();
        double i = user1.getUserMoney();
        double userMoney = userMoney1+user1.getUserMoney();
        user1.setUserMoney(userMoney);
        //日期
        Date day = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd  HH:mm:ss" );
        String str = sdf.format(day);
        moneyDetails.setMdTime(str);
        moneyDetails.setUserId(user1.getUserId());
        moneyDetails.setWorkId(work.getWorkId());
        moneyDetails.setMoney(userMoney1);
        moneyDetails.setMatter("充值");

        int result = operationUserBizImpl.recharge(user1,moneyDetails);
        if (result ==1){
            session.setAttribute("user",user1);
            result1 = "success";
        }else {
            result1 = "error";
        }
        return result1;
    }

    //查看资金明细
    @RequestMapping(value="/seeMoneyDetails.action")
    public String seeMoneyDetails(HttpServletRequest request,String num,MoneyDetails moneyDetails){
        int pageNum = 1;
        if(StringUtils.isNotEmpty(num)){
            if(!num.equals("0")){
                pageNum = Integer.parseInt(num);
            }
//            pageNum = Integer.parseInt(num);
        }
        PageHelper.startPage(pageNum,5);
        User user = (User) request.getSession().getAttribute("user");
        moneyDetails.setUserId(user.getUserId());
        List<MoneyDetails> allMoneyDetails = operationUserBizImpl.getMoneyDetails(moneyDetails);
//        List<MoneyDetails> allMoneyDetails = operationUserBizImpl.getMoneyDetails(user.getUserId());
        PageInfo<MoneyDetails> pageInfo = new PageInfo<>(allMoneyDetails);
        request.setAttribute("pageInfo",pageInfo);
        return "admin/moneyDetails";
    }

    //展示为用户退款页面
    @RequestMapping(value="/showRefund.action")
    public String showRefund(){
        return "admin/refund";
    }

    //退款
    @RequestMapping(value="/refund.action")
    public String refund(HttpServletRequest request,String userId,MoneyDetails moneyDetails){
        String result1 = "error";
        User user1 = (User) request.getSession().getAttribute("user");
        Work work = (Work) request.getSession().getAttribute("work");
        //日期
        Date day = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd  HH:mm:ss" );
        String str = sdf.format(day);
        moneyDetails.setMdTime(str);

        moneyDetails.setUserId(user1.getUserId());
        moneyDetails.setWorkId(work.getWorkId());
        moneyDetails.setMoney(user1.getUserMoney());
        moneyDetails.setMatter("退款");

        user1.setUserMoney(0);
        int result = operationUserBizImpl.updataUserMoney(user1,moneyDetails);
        if (result ==1){
//            session.setAttribute("user",user1);
            result1 = "redirect:/operationUser/seeMoneyDetails.action";
        }else {
            result1 = "error";
        }
        return result1;

//        result = "redirect:/manageWork/showWork.action";
    }



    public OperationUserBizImpl getOperationUserBizImpl() {
        return operationUserBizImpl;
    }

    public void setOperationUserBizImpl(OperationUserBizImpl operationUserBizImpl) {
        this.operationUserBizImpl = operationUserBizImpl;
    }
}
